<template>
    <div class="ErpRecord">

        <div class="publicSearch">
            <div class="searchBar" :style="{height: isMoreSearch?'auto':''}">
                <el-button-group>
                    <el-button @click="getInventoryBalanceData" type="primary" size="mini" icon="el-icon-search">查询</el-button>
                    <el-button @click="clearSearchData" size="mini" icon="el-icon-refresh">重置</el-button>
                </el-button-group>


                <div>
                    <span>类型</span>
                    <el-select v-model="searchData.type" size="mini" filterable placeholder="请选择接口" clearable>
                        <el-option  label="外购入库"  value=1></el-option>
                        <el-option  label="产品入库"  value=2></el-option>
                        <el-option  label="委外加工入库"  value=5></el-option>
                        <el-option  label="其他入库"  value=10></el-option>
                        <el-option  label="调拨单"  value=41></el-option>
                        <el-option  label="销售出库"  value=21></el-option>
                        <el-option  label="生产领料出库"  value=24></el-option>
                        <el-option  label="委外加工出库"  value=28></el-option>
                        <el-option  label="其他出库"  value=29></el-option>
                        <el-option  label="手动创建单据"  value=998></el-option>
                        <el-option  label="质检"  value=999></el-option>
                        <el-option  label="审核"  value=997></el-option>
                    </el-select>
                </div>
                <div>
                    <span>状态</span>
                    <el-select v-model="searchData.state" size="mini" filterable placeholder="请选择状态" clearable>
                        <el-option     label="同步成功"   value=2>   </el-option>
                        <el-option     label="同步失败"   value=1>   </el-option>
                    </el-select>
                </div>
                <div>
                    <span>关键字</span>
                    <el-input v-model="searchData.keyWords" size="mini" placeholder="单号/参数" clearable></el-input>
                </div>
                <div>
                    <span>同步日期</span>
                    <el-date-picker
                            v-model="searchData.startTime"
                            size="mini"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择日期">
                    </el-date-picker>
                </div>
            </div>

            <span class="moreSearchBtn"
                  :class="isMoreSearch?'el-icon-caret-top':'el-icon-caret-bottom'"
                  @click="isMoreSearch = !isMoreSearch"></span>
        </div>

        <div class="headerBtns">
            <!--<el-button
                    size="small"
                    type="primary"
                    round
                    icon="iconfont icon-shangjia"
                    @click="exportExcel()"
            >导出</el-button>-->
        </div>
        <div class="tableBox rowHeight">
            <el-table
                    v-loading="loading"
                    border
                    highlight-current-row
                    :data="inventoryBalances"
                    height="calc(100% - 50px)"
                    @selection-change="handleSelectionChange"
                    style="width: 100%">
                <!-- <el-table-column
                  fixed="left"
                  type="selection"
                  width="55">
                </el-table-column>
                -->

                <!--<el-table-column
                   show-overflow-tooltip
                   prop="type"
                   label="类型"
                   min-width="100">
                   <template slot-scope="scope">
                     <span  v-if="scope.row.type==1" style="color:#409EFF">入库</span>
                     <span  v-if="scope.row.type==2"  style="color:#E6A23C"  >出库</span>
                   </template>
                 </el-table-column>-->
                <el-table-column
                        show-overflow-tooltip
                        prop="type"
                        label="业务类型"
                        min-width="130">
                    <template slot-scope="scope">
                        <div>{{ scope.row.type | billType2str }}</div>
                    </template>
                </el-table-column>
                <el-table-column
                        show-overflow-tooltip
                        prop="state"
                        label="状态"
                        min-width="80">
                    <template slot-scope="scope">
                        <span  v-if="scope.row.state==1" style="color:#F56C6C">同步失败</span>
                        <span  v-if="scope.row.state==2"  style="color:#67C23A"  >同步成功</span>
                    </template>
                </el-table-column>
                <el-table-column
                        show-overflow-tooltip
                        prop="requestTime"
                        label="请求时间"
                        min-width="120">
                </el-table-column>
                <el-table-column
                        show-overflow-tooltip
                        prop="returnTime"
                        label="响应时间"
                        min-width="120">
                </el-table-column>
                <el-table-column
                        show-overflow-tooltip
                        prop="param"
                        label="参数"
                        min-width="230">

                </el-table-column>


                <el-table-column
                        show-overflow-tooltip
                        prop="code"
                        label="code"
                        min-width="50">

                </el-table-column>
                <el-table-column
                        show-overflow-tooltip
                        prop="msg"
                        label="msg"
                        width="230">
                </el-table-column>

            </el-table>
            <el-pagination
                    class="pagination-bottom"
                    style="text-align: right;"
                    @current-change="handleCurrentChange"
                    @size-change="handleSizeChange"
                    :page-sizes="[15,30,50,100]"
                    :current-page.sync="searchData.pageNum"
                    :page-size="searchData.pageSize"
                    layout="sizes, total, prev, pager, next, jumper"
                    :total="searchData.total">
            </el-pagination>
        </div>
    </div>
</template>

<script>
    import DateUtils from "../../../utils/DateUtils";
    export default {
        name: 'ErpRecord',
        data() {
            return {
                loading:false,
                isMoreSearch: false,

                searchData: {
                    pageNum: 1,
                    pageSize: 15,
                    total: null,
                    startTime: '',
                    endTime: '',
                    keyWords: '',
                    //wareId: PF.getLocal('wareId', 'number'),
                    organizationId: null,
                    type:'',
                    state:''

                },

                inventoryBalances: [],
                organizations: [],
                multipleSelection: [],
                freezeReasons: [],

                dialogNewVisible: false,
                dialogUpdateVisible: false,
                dialogFreezeVisible: false,

                formLabelWidth: '120px',

                form: {
                    freezeMaster: {
                        freezeDescribe: '',
                        memo: '',
                        wareId: null,
                        wareName: '',
                        organizationId: null,
                        organizationName: '',
                    },
                    freezeDetails: []
                },
                rules:{
                    freezeDescribe: [
                        { required: true, message: '请输入冻结原因', trigger: 'blur' }
                    ],
                    memo: [
                        { required: true, message: '请输入备注', trigger: 'blur' }
                    ],
                },

            }
        },
        computed: {

        },
        methods: {
            //导出
            exportExcel(){
                let url = localStorage.getItem("baseUrl");
                //let token = this.$tool.localGet("token");
                window.location.href=url+"/erpBack/exportListInfo?type="+this.searchData.type+"&state="+this.searchData.state+
                    "&startTime="+this.searchData.startTime;
            },
            handleSizeChange(val){
                this.searchData.pageSize = val;
                this.getInventoryBalanceData();
            },
            handleCurrentChange(val) {
                this.pageNum = val;
                this.getInventoryBalanceData()
            },
            clearSearchData() {
                this.searchData.type = null;
                this.searchData.state = null;
                this.searchData.startTime = '';
                this.searchData.endTime = '';
            },
            getInventoryBalanceData() {
                let searchData = PF.JSON(this.searchData);
                if(searchData.startTime){
                    let endTimestamp = new Date(searchData.startTime).getTime() + 3600 * 1000 * 24
                    searchData.endTime = DateUtils.timeToStr(endTimestamp,"yyyy-MM-dd")
                }
                this.loading  =true;
                IOT.getServerData('/erpBack/selectList', 'post', searchData, (ret) => {
                    console.log("查询erp回传记录返回：",ret)
                    this.loading  =false;
                    if (ret.code === 200) {
                        let list = ret.rows;

                        this.inventoryBalances = list;
                        this.searchData.total = ret.total ;
                        this.searchData.pageNum = ret.pageNumber
                    } else {
                        IOT.tips(ret.message || '服务器请求失败，稍后再试！', 'error');
                    }
                });
            },
            exportInventory(){
                let searchData = PF.JSON(this.searchData);
                if(!PF.isNull(searchData.itemCode)){
                    delete searchData.itemCode
                }
                if(!PF.isNull(searchData.itemName)){
                    delete searchData.itemName
                }

                searchData.pageSize = searchData.pageSize ;
                IOT.showOverlay('正在导出，请稍等...');


                IOT.getServerData('/erp/records/list', 'get', searchData, (ret) => {
                    IOT.hideOverlay();
                    if (ret.code === 200) {
                        var path = ret.data;

                        window.open(path)
                    } else {
                        IOT.tips(ret.message || '服务器请求失败，稍后再试！', 'error');
                    }
                },true);
            },
            resetForm(formName) { // 重置
                this.$refs[formName].resetFields();
            },


            handleSelectionChange(val) {
                this.multipleSelection = val;
            },


            changePackDetailId(index){

                let detail = PF.JSON(this.inventoryBalances[index]);
                for (let i = 0; i < detail.packDetails.length; i++) {
                    if (detail.packDetails[i].packDetailId1 === detail.packDetailId) {
                        this.inventoryBalances[index].packDescribe = detail.packDetails[i].packDescribe1;
                        this.inventoryBalances[index].transRatio = detail.packDetails[i].transRatio1;
                    }
                }
            },
        },
        created() {
            this.getInventoryBalanceData();

            PF.getOrganizations((rows) => {
                this.organizations = rows;
            });
        },
        mounted() {
        }
    }
</script>

<style scoped lang="less">
    .ErpRecord {

    }
</style>
